<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo-container">
        <h1 class="title">AgriSRM</h1>
        <p class="subtitle">农业供应链资源管理平台</p>
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="用户名"
            :prefix-icon="UserIcon"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="密码"
            :prefix-icon="LockIcon"
            size="large"
            show-password
          />
        </el-form-item>
        <el-form-item class="remember-box">
          <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
          <el-link type="primary" :underline="false" class="forget-pwd">忘记密码</el-link>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :loading="loading" class="login-button" @click="handleLogin">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { User as UserIcon, Lock as LockIcon } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import router from '@/router'

const loading = ref(false)
const loginFormRef = ref(null)

const loginForm = reactive({
  username: '',
  password: '',
  remember: false,
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
  ],
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  try {
    loading.value = true
    await loginFormRef.value.validate()

    // TODO: 这里添加实际的登录逻辑
    setTimeout(() => {
      ElMessage.success('登录成功')
      loading.value = false

      router.push('/operation/dashboard')
    }, 1500)
  } catch (error) {
    loading.value = false
    console.error('登录失败:', error)
  }
}
</script>

<style>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: var(--background-color-base);
  background-image: linear-gradient(
    to bottom right,
    rgba(103, 194, 58, 0.1),
    rgba(103, 194, 58, 0.05)
  );
}

.login-box {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 8px;
  box-shadow: var(--box-shadow-base);
}

.logo-container {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 28px;
  color: var(--primary-color);
  margin-bottom: 8px;
}

.subtitle {
  font-size: 16px;
  color: var(--text-color-regular);
}

.login-form {
  margin-top: 20px;
}

.login-button {
  width: 100%;
  background-color: var(--primary-color);
  border-color: var(--primary-color);
  font-size: 16px;
  height: 44px;
}

.login-button:hover,
.login-button:focus {
  background-color: var(--primary-color-light);
  border-color: var(--primary-color-light);
}

.remember-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.forget-pwd {
  color: var(--primary-color);
}

/* 自定义Element Plus组件样式 */
:deep(.el-input__wrapper) {
  height: 44px;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: var(--primary-color);
  border-color: var(--primary-color);
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: var(--primary-color);
}

:deep(.el-checkbox__inner:hover) {
  border-color: var(--primary-color);
}
</style>
